package org.xb.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xb.vo.Admin;
import org.xb.dao.AdminDAO;
import org.xb.factory.DAOFactory;

public class AdminServlet extends HttpServlet {
	public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		this.doPost(request,response);
	}
	public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
		String path="/error/error.jsp";
		String status=request.getParameter("status");
		if(status==null||"".equals(status)){
		request.getRequestDispatcher(path).forward(request,response);
		}
		if("findLogin".equals(status)){
			this.findLogin(request,response);
		}
		if("loginout".equals(status)){
			this.loginout(request,response);
		}
		if("create".equals(status)){
			this.create(request,response);
		}
		if("list".equals(status)){
			this.list(request,response);
		}
		if("findById".equals(status)){
			this.findById(request,response);
		}
		if("delete".equals(status)){
			this.delete(request,response);
		}
		if("update".equals(status)){
			this.update(request,response);
		}
	}
	


	private void findLogin(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException {
		String path="/error/error.jsp";
		String adname=request.getParameter("adname");
		String adpassword=request.getParameter("adpassword");
		Admin admin=null;
		try{
			admin=DAOFactory.getAdminDAOInstance().findLogin(adname,adpassword);
			if(admin!=null){
			 request.getSession().setAttribute("admin", admin);
			 path="/Active.html";
			}else{
				path="/Admin.html";
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		request.getRequestDispatcher(path).forward(request,response);
	}

	private void loginout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException,IOException{
		String path="/error/error.jsp";
		request.getSession().invalidate();
		path="login.jsp";
		request.getRequestDispatcher(path).forward(request,response);
	}
	private void create(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println(1);
		String path="../errer/error.jsp";
		String adname=request.getParameter("adname");
		String adpassword=request.getParameter("adpassword");
		Admin admin=new Admin();
		admin.setAdname(adname);
		admin.setAdpassword(adpassword);
		try{
			if(DAOFactory.getAdminDAOInstance().doCreate(admin)){
				path="AdminServlet?status=list";
			}
		}catch(Exception e){
				e.printStackTrace();
			
		}
	request.getRequestDispatcher(path).forward(request,response);
}
	private void list(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException {
		String path="../error/error.jsp";
		String keyword=request.getParameter("keyword");
		if(keyword==null||"".equals(keyword)){
			keyword="";
		}
		List<Admin> all=null;
		int currentPage=1;
		int lineSize=0;
		long PageSize=0;
		long allRecorders=0;
		try{
			currentPage=Integer.parseInt(request.getParameter("cp"));
		}catch(Exception e){}
		try{
			//lineSize=DAOFactory.getIConfingDAOINstance().findById(1).getAdmin_linesize();
		    AdminDAO dao=DAOFactory.getAdminDAOInstance();
		    all=dao.listAll(keyword,currentPage,lineSize);
		    allRecorders=dao.getCount(keyword);
		    PageSize=(allRecorders+lineSize-1)/lineSize;
		    request.setAttribute("currentPage",currentPage);
		    request.setAttribute("PageSize",PageSize);
		    request.setAttribute("keyword",keyword);
		    request.setAttribute("all",all);
		    path="/admit/admin_list.jsp";
		}catch(Exception e){
			e.printStackTrace();
		}
		request.getRequestDispatcher(path).forward(request,response);
	}
	private void findById(HttpServletRequest request,
			HttpServletResponse response)throws ServletException,IOException {
		    String path="/error/error.jsp";
		   Integer id=Integer.parseInt(request.getParameter("id"));
		    try{
		    	Admin admin=DAOFactory.getAdminDAOInstance().findById(id);
		    	request.setAttribute("adname", admin.getAdname());
		    	request.setAttribute("adpassword",admin.getAdpassword());
		    	path="/admit/admin_list.jsp";
		    }catch(Exception e){
		    	e.printStackTrace();
		    }
		    request.getRequestDispatcher(path).forward(request,response);
	}
	private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		   String path="/erro/error.jsp";
		   Integer adid=Integer.parseInt(request.getParameter("adid"));
		   String adname=request.getParameter("adname");
		   String adpassword=request.getParameter("adpassword");
		   try{
		     Admin admin=DAOFactory.getAdminDAOInstance().findById(adid);
		     admin.setAdname(adname);
		     admin.setAdpassword(adpassword);
		     if(DAOFactory.getAdminDAOInstance().doUpdate(admin)){
		    	 path="AdminServlet?status=findById&adid="+adid;
		     }
		   }catch(Exception e){
			   e.printStackTrace();
		   }
		   request.getRequestDispatcher(path).forward(request, response);
		   }
	private void update(HttpServletRequest request, HttpServletResponse response) {
		String path="/error/error.jsp";
		Integer id=Integer.parseInt(request.getParameter("id"));
		Admin admin=new Admin();
		
	}
}
